/**
 * 页面公共mixins
 */

const methods = {
  loadEnd(handle) {
    this.isPageLoadError = false;
    if (!this.isPageLoad) {
      this.isEnableRefreshStatus = true;
      this.isPageLoad = true;
      this.$nextTick(() => {
        this.$loading.close();
        handle && handle();
      });
    } else {
      this.$loading.close();
    }
  },
  loadError(handle) {
    this.isEnableRefreshStatus = true;
    this.isPageLoadError = true;
    this.$nextTick(() => {
      this.$loading.close();
      handle && handle();
    });
  },
  forceRefresh() {
    this.isEnableRefreshStatus = false;
    this.isPageLoad = false;
    this.$loading.open({message: this.pageLoadingText});
    this.fetchPage && this.fetchPage();
  },
  load() {
    if (!this.isPageLoad) {
      this.isPageLoad = false;
      this.$loading.open({message: this.pageLoadingText});
    }
  },
  catchPagePromise(promise) {
    return new Promise(resolve => {
      promise.then(data => {
        resolve(data)
      }, err => {
        this.loadError();
      }).catch()
    })
  }
};

const data = function () {
  return {
    isEnableRefreshStatus: false, // 是否可以强制刷新
    PAGE_QUERY: this.$route.query,
    isPageLoad: false, // 页面是否加载完成
    isPageLoadError: false, // 页面是否加载错误
    pageLoadingText: '', // 页面加载提示文字
  }
};

const beforeRouteLeave = function (to, from, next) {
  this.$loading.close();
  next();
};

const created = function () {
  if (!this.isPageLoad) {
    this.load();
    this.fetchPage && this.fetchPage();
  }
};


export default {
  methods,
  data,
  created,
  beforeRouteLeave
}
